package panels;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JToolBar;
import javax.swing.JButton;
import sqlmanager.SQLManager;

public class SQLEditorPanel extends JPanel {
	private final String GoAC = "go";
	private final String PreviousAC = "previous";
	private final String NextAC = "next";

	public JSplitPane splitPane = null;
	private JScrollPane editorScrollPane = null;
	private JScrollPane resultScrollPane = null;
	public JTextArea editor = null;
	public JTable table = null;
	public DataBrowserModel tableModel = null; // @jve:decl-index=0:visual-constraint=""
	public JLabel statusLabel = null;
	private JToolBar toolBar = null;
	private JButton goButton = null;
	private JButton previousButton = null;
	private JButton nextButton = null;

	private SQLManager sqlManager = null;
	private ActionHandler actionHandler = null;
	private LinkedList results = new LinkedList();

	/**
	 * This is the default constructor
	 */
	public SQLEditorPanel(SQLManager sqlManager) {
		super();
		this.sqlManager = sqlManager;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		statusLabel = new JLabel();
		statusLabel.setText("״̬��"); // Generated
		this.setLayout(new BorderLayout()); // Generated
		this.setSize(300, 200);
		this.add(getToolBar(), java.awt.BorderLayout.NORTH); // Generated
		this.add(getSplitPane(), java.awt.BorderLayout.CENTER); // Generated
		this.add(statusLabel, java.awt.BorderLayout.SOUTH); // Generated
	}

	/**
	 * This method initializes splitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = new JSplitPane();
			splitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); // Generated
			splitPane.setDividerSize(5); // Generated
			splitPane.setTopComponent(getEditorScrollPane()); // Generated
			splitPane.setBottomComponent(getResultScrollPane()); // Generated
			splitPane.setResizeWeight(0.5D); // Generated
		}
		return splitPane;
	}

	/**
	 * This method initializes editorScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getEditorScrollPane() {
		if (editorScrollPane == null) {
			editorScrollPane = new JScrollPane();
			editorScrollPane.setViewportView(getEditor()); // Generated
		}
		return editorScrollPane;
	}

	/**
	 * This method initializes resultScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getResultScrollPane() {
		if (resultScrollPane == null) {
			resultScrollPane = new JScrollPane();
			resultScrollPane.setViewportView(getTable()); // Generated
		}
		return resultScrollPane;
	}

	/**
	 * This method initializes toolBar
	 * 
	 * @return javax.swing.JToolBar
	 */
	private JToolBar getToolBar() {
		if (toolBar == null) {
			toolBar = new JToolBar();
			toolBar.setPreferredSize(new java.awt.Dimension(45, 32)); // Generated
			toolBar.add(getGoButton()); // Generated
			toolBar.add(new JToolBar.Separator(new Dimension(16, 16)));
			toolBar.add(getPreviousButton()); // Generated
			toolBar.add(getNextButton()); // Generated
		}
		return toolBar;
	}

	/**
	 * This method initializes goButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getGoButton() {
		if (goButton == null) {
			goButton = new JButton();
			goButton.setMnemonic(java.awt.event.KeyEvent.VK_G); // Generated
			goButton.setText("Go"); // Generated
			goButton.setActionCommand(this.GoAC);
			goButton.addActionListener(this.getActionHandler());
		}
		return goButton;
	}

	/**
	 * This method initializes previousButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getPreviousButton() {
		if (previousButton == null) {
			previousButton = new JButton();
			previousButton.setMnemonic(java.awt.event.KeyEvent.VK_P); // Generated
			previousButton.setText("Previous"); // Generated
			previousButton.setActionCommand(this.PreviousAC);
			previousButton.addActionListener(this.getActionHandler());
		}
		return previousButton;
	}

	/**
	 * This method initializes nextButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getNextButton() {
		if (nextButton == null) {
			nextButton = new JButton();
			nextButton.setMnemonic(java.awt.event.KeyEvent.VK_N); // Generated
			nextButton.setText("Next"); // Generated
			nextButton.setActionCommand(this.NextAC);
			nextButton.addActionListener(this.getActionHandler());
		}
		return nextButton;
	}

	/**
	 * This method initializes editor
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getEditor() {
		if (editor == null) {
			editor = new JTextArea();
		}
		return editor;
	}

	/**
	 * This method initializes table
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.setModel(getTableModel()); // Generated
		}
		return table;
	}

	/**
	 * This method initializes topicTableModel
	 * 
	 * @return table.DataBrowserModel
	 */
	private DataBrowserModel getTableModel() {
		if (tableModel == null) {
			tableModel = new DataBrowserModel();
		}
		return tableModel;
	}

	public void clear() {
		this.editor.setText("");
		this.tableModel.clear();
	}

	public void enableAll() {
		this.editor.setEditable(true);
		this.table.setEnabled(true);
		this.splitPane.setEnabled(true);
		this.toolBar.setEnabled(true);
		this.goButton.setEnabled(true);
		this.previousButton.setEnabled(true);
		this.nextButton.setEnabled(true);
	}

	public void disableAll() {
		this.editor.setEditable(false);
		this.table.setEnabled(false);
		this.splitPane.setEnabled(false);
		this.toolBar.setEnabled(false);
		this.goButton.setEnabled(false);
		this.previousButton.setEnabled(false);
		this.nextButton.setEnabled(false);
	}

	private void showResultSet(ResultSet rs) {
		try {
			this.sqlManager.getTableContents(rs, this.tableModel.contents);
			this.sqlManager.getFieldNames(rs, this.tableModel.fieldNames);
			this.tableModel.fireTableStructureChanged();
		} catch (SQLException e) {
			System.out.println("����ResultSetʱ�������");
			e.printStackTrace();
		}
	}

	private void excuteSql(String sql) {
		this.results.clear();
		boolean hasResultSets = false;
		try {
			hasResultSets = this.sqlManager.stmt.execute(sql);
			if (true == hasResultSets) {
				// �н��
				// do
				// {
				// this.results.add( this.sqlManager.stmt.getResultSet() );
				// }while( this.sqlManager.stmt.getMoreResults() );
				// this.statusLabel.setText( "�� " + results.size() + " ���Ľ��"
				// );
				// this.showResultSet( (ResultSet) results.getFirst() );
				this.showResultSet(this.sqlManager.stmt.getResultSet());
			}
		} catch (SQLException e) {
			// ִ��sql�������
			SQLException oldEx = e;
			StringBuffer errors = new StringBuffer("\n--*******************" + "\n--ִ��SQLʱ�������:");
			do {
				errors.append("\n--Code: " + e.getErrorCode() + ": " + e.getSQLState());
				e = e.getNextException();
			} while (null != e);
			errors.append("\n--*******************");
			this.editor.append(errors.toString());
			this.statusLabel.setText("�������!");
			oldEx.printStackTrace();
		}
	}

	private ActionHandler getActionHandler() {
		if (null == actionHandler)
			actionHandler = new ActionHandler();
		return actionHandler;
	}

	class ActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String ac = e.getActionCommand();
			if (ac.equals(GoAC)) {
				excuteSql(editor.getText().trim());
			} else if (ac.equals(PreviousAC)) {
			} else if (ac.equals(NextAC)) {
			}
		}
	}
}
